﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using m.Channelvn.Cached.Common;
using EntityFramework;
using m.Channelvn.Cached.UrlRewrite;
using System.Text;
using BO;

namespace m.Website.Controls.Touch
{
    public partial class ListProduct : System.Web.UI.UserControl
    {
        protected const int PageSize = 9;
        protected bool noItem = true;
        private const string ProductFocusItemFormat = @"<div class=""item mb30 {11} {0}"" style=""width:97%"">
                    <div class=""img"">
                        <a href=""{4}"" title=""{3}""><img title=""{10}"" alt=""{3}"" src=""{5}"" /></a>
                    </div>
                    <div class=""content-right"">
                        {1}<div class=""name""><a href=""{4}"" title=""{3}"">{2}</a></div>
                        <div class=""code-name"">{6}</div>
                        <div class=""desc"">{7}</div>
                        <div class=""shopping"">
                            <a href=""javascript:void(0);"" class=""cart""></a>
                            <div class=""price"">
                                <div class=""current"">{8}</div>
                                <div class=""discount"">{9}</div>
                            </div>
                        </div>
                    </div>    
                </div>";
        private const string ProductNewFormat = "<i class=\"new\"></i>";
        protected int pageIndex, CompanyID, ZoneID, pageType,iZoneid = 9999;
        protected string catUrl;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                catUrl = Lib.QueryString.CatUrl;
                pageIndex = Lib.QueryString.PageIndex;
                pageType = Lib.QueryString.PageType;
                CompanyID = Lib.QueryString.CompanyID;
                ZoneID = Lib.QueryString.ZoneID;
                if (string.IsNullOrEmpty(catUrl)) _301Redirection.RedirectTo("/404.aspx");
                ZoneProduct obj;
                if (pageType == 7) // List Product Company
                {
                    obj = ZoneProductBO.GetZoneProductById(ZoneID);
                    if (obj != null)
                    {
                        #region nav
                        {
                            var sb = new StringBuilder();
                            //sb.AppendFormat("<a href='/c/{0}.htm' title='{1}' >{2}</a> > ", obj.url, HttpUtility.HtmlEncode(obj.title), obj.title);
                            sb.AppendFormat("<a href='/{0}.htm' title='{1}' >{2}</a> > ", obj.url, HttpUtility.HtmlEncode(obj.title), obj.title);
                            var objChild = CompaniesBO.GetCompaniesByID(CompanyID);
                            if (objChild != null)
                                sb.AppendFormat("<a href='/c/{0}.htm' title='{1}' >{2}</a> ", ZoneID + "/" + CompanyID + "/" + HttpUtility.HtmlEncode(objChild.NameCp), HttpUtility.HtmlEncode(objChild.NameCp), objChild.NameCp);
                            ltrNav.Text = sb.ToString();
                        }
                        #endregion
                    }
                }
                else
                {
                    obj = ZoneProductBO.GetZoneProductByCatUrl(catUrl);
                    if (obj != null)
                    {
                        #region nav

                        {
                            var sb = new StringBuilder();
                            if (obj.ParentId != null && obj.ParentId > 0)
                            {
                                var objChild = ZoneProductBO.GetZoneProductById(obj.ParentId.Value);
                                if (objChild != null)
                                    sb.AppendFormat("<a href='/{0}.htm' title='{1}' >{2}</a> > ", objChild.url, HttpUtility.HtmlEncode(objChild.title), objChild.title);
                            }
                            sb.AppendFormat("<a href='/{0}.htm' title='{1}' >{2}</a>", obj.url, HttpUtility.HtmlEncode(obj.title), obj.title);

                            ZoneProduct zd = ZoneProductBO.GetZoneProductDetailByUrl(catUrl);
                            ltrNav.Text = sb.ToString();
                        }
                        #endregion


                    }
                }

                #region 2 product top
                {
                    //rptProductTopFocus.DataSource = ProductsBO.GetTopProductIsFocusHome(2, obj.ZoneId,CompanyID);
                    //rptProductTopFocus.DataBind();
                }
                #endregion

                #region list product
                {
                    if (catUrl == "danh-sach-san-pham")
                    {
                        //show all sp
                        iZoneid = 9999;
                    }
                    else
                    {
                        //show sp theo dk
                        iZoneid = obj.ZoneId;
                    }
                    var lst = ProductsBO.GetPagingProductByZoneProduct(pageIndex, PageSize, iZoneid, CompanyID);
                    if (lst.Any())
                    {
                        noItem = false;
                        var sb = new StringBuilder();
                        var count = 1;
                        foreach (var item in lst)
                        {
                            var cls = count % 3 == 0 ? "mr0" : string.Empty;
                            long date = 0;
                            long.TryParse(item.EndDateNew, out date);
                            var pNew = date > 0 && date >= long.Parse(DateTime.Now.ToString("yyyyMMdd")) ? ProductNewFormat : string.Empty;

                            var price = item.TypeSale == 1 ? "Giá: Liên hệ" : Utils.ConvertMoneyNice(item.price.ToString(), BO.Const.FormatMoney);
                            var sale = item.TypeSale == 1 ? string.Empty : Utils.ConvertMoneyNice(item.sale.ToString(), BO.Const.FormatMoney);
                            var sapo = item.Sapo.ToString();
                            if (int.Parse(item.sale.ToString()) == 0)
                            {
                                sale = "";
                            }
                            sb.AppendFormat(ProductFocusItemFormat, cls
                                , pNew
                                , item.Title
                                , HttpUtility.HtmlEncode(item.Title)
                                , item.Url
                                , BO.Utils.CropImageMobile(172, 172, BO.Utils.GetOneImagesByArr(item.Avatar))
                                , item.ProductCode
                                , BO.Utils.getSubStringNice(item.Sapo, 50)
                                , price
                                , sale
                                , sapo
                                , count % 2 == 0 ? "even" : string.Empty);
                            count++;
                        }
                        if (lst.Count > 0)
                        {
                            ltrListFocus.Text = sb.ToString();
                        }
                        else
                        {
                            ltrListFocus.Text = "<h1>Hiện chưa có sản phẩm nào ở danh mục này</h1>";
                        }
                    }
                }
                #endregion

                
            }
            catch (Exception)
            {

                ltrNav.Text = "Hiện chưa có sản phẩm";
            }
        }
    }
}